package ssm.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import ssm.dao.IRoleDao;
import ssm.domain.Permission;
import ssm.domain.Role;
import ssm.service.IRoleService;

import java.util.List;

@Service
@Transactional
public class RoleServiceImpl implements IRoleService {

    @Autowired
    private IRoleDao roleDao;

    @Override
    public List<Role> findAll() throws Exception {
        return roleDao.findAll();
    }

    @Override
    public void findAll(Role role) throws Exception{
        roleDao.save(role);
    }

    @Override
    public Role findById(String id) throws Exception {
        return roleDao.findById(id);
    }

    @Override
    public void deleteRole(String id) {
        /*此处由于Role表和user表、permission表均有关联，因此应该依次删除*/
        roleDao.deleteFromUser_RoleById(id);

        roleDao.deleteFromRole_PermissionById(id);

        roleDao.deleteRoleById(id);
    }

    @Override
    public Role findByRoleId(String roleId) {
        return roleDao.findByRoleId(roleId);
    }

    @Override
    public List<Permission> findOtherPermissions(String roleId) {
        return roleDao.findOtherPermissions(roleId);
    }

    @Override
    public void addPermissionToRole(String roleId, String[] permissionIds) {
        /*将每个permissionId对应的信息均添加*/
        for (String permissionId : permissionIds) {
            roleDao.addPermissionToRole(roleId,permissionId);
        }
    }
}
